package com.ruoyi.qiuyue.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.qiuyue.common.UserConstat;
import com.ruoyi.qiuyue.handler.exception.UserExistsException;
import com.ruoyi.qiuyue.handler.exception.UserNullException;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.qiuyue.mapper.MusicUserMapper;
import com.ruoyi.qiuyue.domain.MusicUser;
import com.ruoyi.qiuyue.service.IMusicUserService;

/**
 * 秋月用户Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-15
 */
@Service
public class MusicUserServiceImpl implements IMusicUserService 
{
    @Autowired
    private MusicUserMapper musicUserMapper;

    /**
     * 查询秋月用户
     * 
     * @param id 秋月用户主键
     * @return 秋月用户
     */
    @Override
    public MusicUser selectMusicUserById(Long id)
    {
        return musicUserMapper.selectMusicUserById(id);
    }

    /**
     * 查询秋月用户列表
     * 
     * @param musicUser 秋月用户
     * @return 秋月用户
     */
    @Override
    public List<MusicUser> selectMusicUserList(MusicUser musicUser)
    {
        return musicUserMapper.selectMusicUserList(musicUser);
    }

    /**
     * 新增秋月用户
     * 
     * @param musicUser 秋月用户
     * @return 结果
     */
    @Override
    public int insertMusicUser(MusicUser musicUser)
    {
        MusicUser query = new MusicUser();
        query.setAccount(musicUser.getAccount());
        MusicUser oldMusicUser = musicUserMapper.selectMusicUserBySelf(query);
        if ( null != oldMusicUser) {
            throw new UserExistsException();
        }
        musicUser.setCreateTime(DateUtils.getNowDate());
        musicUser.setUpdateTime(DateUtils.getNowDate());
        musicUser = entryPassword(musicUser);
        return musicUserMapper.insertMusicUser(musicUser);
    }

    /**
     * 修改秋月用户
     * 
     * @param musicUser 秋月用户
     * @return 结果
     */
    @Override
    public int updateMusicUser(MusicUser musicUser)
    {
        if(!StringUtils.isEmpty(musicUser.getAccount())) {
            MusicUser query = new MusicUser();
            query.setAccount(musicUser.getAccount());
            MusicUser oldMusicUser = musicUserMapper.selectMusicUserBySelf(query);
            if(null == oldMusicUser || oldMusicUser.getDel() ) {
                throw new UserNullException();
            }
        }
        musicUser = entryPassword(musicUser);
        musicUser.setUpdateTime(DateUtils.getNowDate());
        return musicUserMapper.updateMusicUser(musicUser);
    }

    /**
     * 批量删除秋月用户
     * 
     * @param ids 需要删除的秋月用户主键
     * @return 结果
     */
    @Override
    public int deleteMusicUserByIds(Long[] ids)
    {
        return musicUserMapper.deleteMusicUserByIds(ids);
    }

    /**
     * 删除秋月用户信息
     * 
     * @param id 秋月用户主键
     * @return 结果
     */
    @Override
    public int deleteMusicUserById(Long id)
    {
        return musicUserMapper.deleteMusicUserById(id);
    }

    /**
     * 根据账号和密码登录
     * @param account
     * @param password
     * @return
     */
    @Override
    public MusicUser getMusicUserByAccount(String account, String password) {
        MusicUser query = new MusicUser();
        query.setAccount(account);
        query.setPassword(password);
        query = entryPassword(query);
        MusicUser musicUsers = musicUserMapper.selectMusicUserBySelf(query);
        if (null == musicUsers) {
            throw new UserNullException(UserConstat.USER_LOGIN_ERROR);
        }
        return musicUsers;
    }

    /**
     * 修改密码
     * @param account
     * @param password
     * @return
     */
    @Override
    public MusicUser forgetUserByAccount(String account, String password) {
        // 查找用户
        MusicUser query = new MusicUser();
        query.setAccount(account);
        MusicUser oldMusicUser = musicUserMapper.selectMusicUserBySelf(query);
        if(null == oldMusicUser || oldMusicUser.getDel()) {
            throw new UserNullException();
        }
        // 修改密码
        MusicUser updateUser = oldMusicUser;
        updateUser.setPassword(password);
        updateMusicUser(updateUser);
        return updateUser;
    }

    /**
     * 加密用戶密碼
     * @param musicUser
     * @return
     */
    private MusicUser entryPassword(MusicUser musicUser) {
        if (!StringUtils.isEmpty(musicUser.getPassword())) {
            String entry = DigestUtils.md5Hex(musicUser.getPassword());;
            musicUser.setPassword(entry);
        }
        return musicUser;
    }
}
